package com.zy.server.util;

import com.baidu.aip.contentcensor.AipContentCensor;
import org.json.JSONObject;

/**
 * @author 张鑫
 * @date 2020/11/27 19:19
 */
public class BaidCensorUtil {

    //设置APPID/AK/SK
    public static final String APP_ID = "17214730";
    public static final String API_KEY = "WnrcZnQNbveI7UG7sDroOm1K";
    public static final String SECRET_KEY = "FUui3jN440l9eLN8B335C3VS62HvtafU";
    // 初始化一个AipContentCensor
    private static AipContentCensor client;
    static {
        client= new AipContentCensor(APP_ID, API_KEY, SECRET_KEY);
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
    }

    // 图片审核
    public static boolean censorImg(byte[] data) {
        JSONObject res = client.imageCensorUserDefined(data, null);
        if ("合规".equals(res.getString("conclusion"))) {
            return true;
        } else {
            return false;
        }
    }

    // 文本审核
    public static boolean censorText(String msg) {
        JSONObject res = client.textCensorUserDefined(msg);
        System.out.println(res);
        if("合规".equals(res.getString("conclusion"))){
            return true;
        }
        return false;
    }
}
